//
//  MBProgressHUD+HS.m
//  HSToolKit
//
//  Created by huang on 2018/8/3.
//

#import "MBProgressHUD+HS.h"

@implementation MBProgressHUD (HS)

#pragma mark - 显示带有图标的信息
#pragma mark 显示错误信息
+ (void)hs_showError:(NSString *)error
{
    [MBProgressHUD hs_showError:error toView:nil];
}

+ (void)hs_showError:(NSString *)error toView:(UIView *)view
{
    [self hs_showCustomIcon:@"mb_error@2x.png" title:error toView:view isBundle:YES];
}

#pragma mark 显示成功信息
+ (void)hs_showSuccess:(NSString *)success
{
    [MBProgressHUD hs_showSuccess:success toView:nil];
}

+ (void)hs_showSuccess:(NSString *)success toView:(UIView *)view
{
    [self hs_showCustomIcon:@"mb_success@2x.png" title:success toView:view isBundle:YES];
}

#pragma mark 显示信息
+ (void)hs_showInfo:(NSString *)info
{
    [MBProgressHUD hs_showInfo:info toView:nil];
}

+ (void)hs_showInfo:(NSString *)info toView:(UIView *)view
{
    [self hs_showCustomIcon:@"mb_info@2x.png" title:info toView:view isBundle:YES];
}

#pragma mark 显示警告信息
+ (void)hs_showWarn:(NSString *)warn
{
    [MBProgressHUD hs_showWarn:warn toView:nil];
}

+ (void)hs_showWarn:(NSString *)warn toView:(UIView *)view
{
    [self hs_showCustomIcon:@"mb_warning@2x.png" title:warn toView:view isBundle:YES];
}

#pragma mark 显示一些信息
+ (MBProgressHUD *)hs_showMessage:(NSString *)message
{
    return [MBProgressHUD hs_showMessage:message toView:nil];
}

+ (MBProgressHUD *)hs_showMessage:(NSString *)message toView:(UIView *)view
{
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.label.font = [UIFont fontWithName:@"Heiti SC" size:15];
    hud.label.numberOfLines = 0;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    //代表需要蒙版效果
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:.2f];
    return hud;
}

//加载视图
+ (void)hs_showLoading
{
    [self hs_showLoadToView:nil];
}

+ (void)hs_showLoadToView:(UIView *)view
{
    [self hs_showProgressToView:view text:nil];
}

/**
 *  进度条View
 */
+ (MBProgressHUD *)hs_showProgressToView:(UIView *)view text:(NSString *)message
{
    [self hs_hideHUD];
    
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = message;
    hud.label.font = [UIFont fontWithName:@"Heiti SC" size:15];
    hud.label.numberOfLines = 0;
    return hud;
}

//快速显示一条提示信息
+ (void)hs_showAutoMessage:(NSString *)message
{
    [self hs_showAutoMessage:message toView:nil];
}

//自动消失提示，无图
+ (void)hs_showAutoMessage:(NSString *)message toView:(UIView *)view
{
    [self hs_showMessage:message toView:view remainTime:1 model:MBProgressHUDModeText];
}

//自定义停留时间，有图
+(void)hs_showIconMessage:(NSString *)message toView:(UIView *)view remainTime:(CGFloat)time
{
    [self hs_showMessage:message toView:view remainTime:time model:MBProgressHUDModeIndeterminate];
}

//自定义停留时间，无图
+(void)hs_showMessage:(NSString *)message toView:(UIView *)view remainTime:(CGFloat)time
{
    [self hs_showMessage:message toView:view remainTime:time model:MBProgressHUDModeText];
}

+(void)hs_showMessage:(NSString *)message toView:(UIView *)view remainTime:(CGFloat)time model:(MBProgressHUDMode)model
{
    [self hs_hideHUD];
    
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.numberOfLines = 0;
    hud.label.text = message;
    hud.label.font = [UIFont fontWithName:@"Heiti SC" size:15];
    //模式
    hud.mode = model;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // 代表需要蒙版效果
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:.2f];
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // X秒之后再消失
    [hud hideAnimated:YES afterDelay:time];
}

+ (void)hs_showCustomIcon:(NSString *)iconName title:(NSString *)title toView:(UIView *)view
{
    [self hs_showCustomIcon:iconName title:title toView:view isBundle:NO];
}

+ (void)hs_showCustomIcon:(NSString *)iconName title:(NSString *)title toView:(UIView *)view isBundle:(BOOL)isBundle
{
    [self hs_hideHUD];
    
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = title;
    hud.label.numberOfLines = 0;
    hud.label.font = [UIFont fontWithName:@"Heiti SC" size:15];
    // 设置图片
    if (isBundle == YES) {
        UIImage *normalImage = [UIImage imageWithContentsOfFile:[HSProgressHUDImagePath imageBundlePathWithIconName:iconName]];
        hud.customView = [[UIImageView alloc] initWithImage:normalImage];
    }else {
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
    }
    
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:1];
}

+ (void)hs_hideHUDForView:(UIView *)view
{
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
    [self hideHUDForView:view animated:YES];
}

+ (void)hs_hideHUD
{
    [self hs_hideHUDForView:nil];
}

@end

@implementation HSProgressHUDImagePath

+ (NSString *)imageBundlePathWithIconName:(NSString *)iconName
{
    NSString *curBundleDirectory = @"MBProgressHUD.bundle";
    NSString *normalImgPath = [[NSBundle bundleForClass:[self class]] pathForResource:iconName ofType:nil inDirectory:curBundleDirectory];
    return normalImgPath;
}

@end
